//
//  PushTransitionAnimator.m
//  us
//
//  Created by karry on 2025/3/31.
//
#import "PushTransitionAnimator.h"

 
#import "PushTransitionAnimator.h"

@implementation PushTransitionAnimator

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 1.0; // 让动画稍微长一点
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *containerView = [transitionContext containerView];

    UIView *toView = toVC.view;
    toView.alpha = 0;
    [containerView addSubview:toView];

    // 创建多个 View 的快照
    NSMutableArray<UIView *> *snapshots = [NSMutableArray array];

    for (NSUInteger i = 0; i < self.sourceViews.count; i++) {
        UIView *sourceView = self.sourceViews[i];
        UIView *destinationView = self.destinationViews[i];

        UIView *snapshot = [sourceView snapshotViewAfterScreenUpdates:NO];
 
        snapshot.frame = [sourceView convertRect:sourceView.bounds toView:containerView];
        [containerView addSubview:snapshot];
        [snapshots addObject:snapshot];

        // 隐藏原视图
        sourceView.hidden = YES;
        
        if (i == 3) {
            self.phoneLab.frame = snapshot.frame;
            self.phoneLab.alpha = 0;
            [containerView addSubview:self.phoneLab];
        }
        
        destinationView.hidden = YES;
    }

    // **使用 Spring 动画**
    [UIView animateWithDuration:[self transitionDuration:transitionContext]
                          delay:0
         usingSpringWithDamping:0.7
          initialSpringVelocity:1.0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
        for (NSUInteger i = 0; i < snapshots.count; i++) {
            UIView *snapshot = snapshots[i];
            UIView *destinationView = self.destinationViews[i];
            snapshot.frame = [destinationView convertRect:destinationView.bounds toView:containerView];
            
          
            if (i == 1) {  // 特别处理按钮的动画
                // 透明度动画
                snapshot.alpha = 0.6;
            }else if(i == 2){
                snapshot.alpha = 0;
            }else if (i == 3){
                snapshot.alpha = 0;
                
                self.phoneLab.frame = snapshot.frame;
                self.phoneLab.alpha = 1;
            }
        }

        toView.alpha = 1;
    } completion:^(BOOL finished) {
        for (NSUInteger i = 0; i < snapshots.count; i++) {
            UIView *sourceView = self.sourceViews[i];
            UIView *destinationView = self.destinationViews[i];

            sourceView.hidden = NO;
            if (i == 0 || i == 2) {

            }else{
                destinationView.hidden = NO;
            }
            [snapshots[i] removeFromSuperview];
        }
        self.phoneLab.hidden = YES;
        [transitionContext completeTransition:YES];
    }];
}


- (UILabel *)phoneLab {
    if (_phoneLab == nil) {
        _phoneLab = [[UILabel alloc]init];
        _phoneLab.text = @"发送验证码";
        _phoneLab.textAlignment = NSTextAlignmentCenter;
        _phoneLab.textColor = [UIColor whiteColor];
        _phoneLab.font = [UIFont systemFontOfSize:16];
    }
    return _phoneLab;
}

@end

